package com.wangsd.system.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wangsd.common.controller.AjaxResult;
import com.wangsd.common.controller.BaseController;
import com.wangsd.common.utils.ServletUtils;
import com.wangsd.system.entity.SysMenu;
import com.wangsd.system.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 菜单Controller
 *
 * @author wsd
 * @date 2021-03-16
 */
@Api(tags = "菜单管理")
@RestController
@RequestMapping("/system/menu")
public class SysMenuController extends BaseController {
    @Autowired
    private SysMenuService sysMenuService;

    @ApiOperation(value = "查询菜单列表")
    @GetMapping("/list")
    public AjaxResult<PageInfo<SysMenu>> list() {
        Page page = PageHelper.startPage(ServletUtils.getParameterToInt("pageNum"), ServletUtils.getParameterToInt("pageSize"));
        List<SysMenu> list = sysMenuService.selectSysMenuList();
        return AjaxResult.success(getPageInfo(page, list));
    }

    @ApiOperation(value = "获取菜单详细信息")
    @GetMapping("/{id}")
    public AjaxResult<SysMenu> getInfo(@PathVariable("id") Long id) {
        SysMenu sysMenu = sysMenuService.selectSysMenuById(id);
        return AjaxResult.success(sysMenu);
    }

    @ApiOperation(value = "新增/修改菜单")
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@RequestBody SysMenu sysMenu) {
        if (sysMenu.getPid() == null) {
            sysMenu.setMenuType(1);
        } else {
            sysMenu.setMenuType(2);
        }
        if (sysMenu.getId() == null) {
            sysMenuService.insertSysMenu(sysMenu);
        } else {
            sysMenuService.updateSysMenu(sysMenu);
        }
        return AjaxResult.success("success");
    }


    @ApiOperation(value = "删除菜单")
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        sysMenuService.deleteSysMenuById(id);
        return AjaxResult.success("success");
    }

    @ApiOperation(value = "获取所有一级菜单")
    @GetMapping("/selectParentMenu")
    public AjaxResult<List<SysMenu>> selectParentMenu() {
        List<SysMenu> list = sysMenuService.selectMenuByType(1);
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "角色编辑查询该角色现有哪些权限")
    @GetMapping("/getByRoleId/{roleId}")
    public AjaxResult<List<SysMenu>> getByRoleId(@PathVariable("roleId") Long roleId) {
        List<SysMenu> list = sysMenuService.selectSysMenuByRoleId(roleId);
        return AjaxResult.success(list);
    }

    @ApiOperation(value = "查询所有菜单")
    @GetMapping("/getAll")
    public AjaxResult<List<SysMenu>> getAll() {
        List<SysMenu> list = sysMenuService.selectSysMenuList();
        return AjaxResult.success(list);
    }
}
